<?php include '_init_.php';?>
<?php include 'page_header.php';?>
<?php
	$id = isset($_REQUEST['id'])?intval($_REQUEST['id']):0;
	if($id < 1)
		exit("Please specify ID");
	$orderManager = Order_Manager::getOrderManager($_REQUEST['id']);
	if(!$orderManager)
		exit('Data not exist anymore');
	if($orderManager->getStatus() != Order_Manager::$STATUS_CREATING)
	{
			exit("Order doesn't editable");
	}
	$orderManager->init();
	
	$act = isset($_REQUEST['act'])?$_REQUEST['act']:'';
	if($act == 'save')
	{
		$_REQUEST['number'] = trim($_REQUEST['number']);
		$orderManager->setMainInfo($_REQUEST);
		$orderManager->persistOrderInfo();
		header('Location:add-cosc.php?&id='.$orderManager->getId());
		exit();
	}else if($act == 'ajaxchecknumbertaken')
	{
		ob_clean();
		$number_taken = $orderManager->isOrderNumberTaken(trim($_REQUEST['number']));
		if($number_taken)
		{
			echo 'No';
		}else{
			echo 'ok';
		}
		exit();
	}
	
	$pageInfo = $orderManager->getMainInfo();
	
$dtLocation = new DataTable(constant('DB_TABLE_LOCATION'));
$location_list = $dtLocation->get_list('','name');
$dtWorker = new DataTable(constant('DB_TABLE_WORKER'));
$workder_list = $dtWorker->get_list('`type`=1','name');
?>
<script language="javascript">
$(function(){
	$("input[type=text][name=number]").val('<?php echo $pageInfo['number'];?>');
	$("input[type=radio][name=department][value=<?php echo $pageInfo['department'];?>]").attr('checked',true);
	$("input[type=radio][name=department_worker][value=<?php echo $pageInfo['department_worker'];?>]").attr('checked',true);
});
</script>
<div id="main"><!-- start #main //-->
<form method="post">
<input type="hidden" name="id" value="<?php echo $orderManager->getId();?>"/>
<input type="hidden" name="act" value="save"/>
<div id="tita">
  <div class="tita-1">
    <h3>預約單項目</h3>
    <img src="images/point-1.gif" width="18" height="18" /> 
    <h4>預約單設定</h4>
    </div>
  <div class="tita-2"></div>
  </div>
  <div id="mainf" class="clearfix"><!-- start #mainf //-->
  <div class="mainf-1">
    <ul>
      <li class="mainf-1a">預約門市</li>
      <li class="mainf-1b"><img src="images/line5.gif" width="676" height="7" /></li>
      <?php foreach($location_list as $tmpData):?>
      	<li class="mainf-1c"><input name="department" type="radio" class="mainf-radiobtn"  value="<?php echo $tmpData['name'];?>" /><?php echo $tmpData['name'];?></li>
      <?php endforeach;?>
     </ul>
    </div>
    
    <div class="mainf-1" >
    <ul>
     <li class="mainf-1a">單號確認</li>
      <li class="mainf-1b"><img src="images/line5.gif" width="676" height="7" /></li>
      <li class="mainf-1a">預約單號 <input name="number" type="text"  class="maine-keyinf"/></li>
      </ul>
     </div>
    <div class="mainf-1">
    <ul>
      <li class="mainf-1a">門市人員</li>
      <li class="mainf-1b"><img src="images/line5.gif" width="676" height="7" /></li>
      <?php foreach($workder_list as $tmpData):?>
      	<li class="mainf-1c"><input name="department_worker" type="radio" class="mainf-radiobtn"  value="<?php echo $tmpData['name'];?>" /><?php echo $tmpData['name'];?></li>
      <?php endforeach;?>
    </ul>
    </div>
    </div><!-- start #mainf //-->
    
     
    <script language="javascript">
    function submitForm()
    {
    	$('.form_value_error').removeClass('form_value_error');
    	var form = document.forms[0];
    	if(form.number.value == '')
    	{
    		alert('請綠入訂單號!');
    		$(form.number).addClass('form_value_error');
    		return false;
    	}
    	//check order number taken
    	$.get('add-cosb.php?act=ajaxchecknumbertaken&id=<?php echo $orderManager->getId();?>&number='+form.number.value,function(data){
    		if(data != 'ok')
    		{
    			alert("預約單號已存在，訂單號不能够重復");
    		}else{
    			form.submit();
    		}
    	});
    	return false;
    }
    </script>
    <div id="paged"><!-- start #pagec //-->
    <a href="add-cos.php?id=<?php echo $orderManager->getId();?>" ><img src="images/pagec2-prev.gif" width="77" height="29" /></a><a href="#" onclick="return submitForm();"><img src="images/pagec2-next.gif" width="77" height="29" /></a>
  </div><!-- end #paged //-->
</div><!-- end #main //-->
</form>
</div><!-- end #main //-->
<?php include 'page_footer.php';?>